package gentle.support.duplicateSubmission;

import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author:wwz
 */
@Aspect
@Component
public class LogProxy {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final static Object PRESENT = new Object();

    public static String keys = "null";

    @Pointcut("@annotation(gentle.support.duplicateSubmission.Resubmit)")
    public void beforpointcut() {
    }

    private final static class Holder {
        private static HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    @Before("beforpointcut()&& @annotation(resubmit)")
    public Object beforMethod(Resubmit resubmit) throws Throwable {
//        String key = LogProxy.Holder.request.getRequestURI() +  LogProxy.keys;
//        boolean lock = true;
//        try {
//            //设置解锁key
//            lock = ResubmitLock.getInstance().lock(key, PRESENT);
//            if (lock) {
//                //放行
//                return "响应重复提交异常";
//            } else {
//                //响应重复提交异常
//                logger.error("重复提交【代码未执行】");
//                System.out.println("重复提交【代码未执行】");
               return "响应重复提交异常";
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "响应重复提交异常";
//        } finally {
//            //设置解锁key和解锁时间
//            ResubmitLock.getInstance().unLock(lock, key, 10);
//        }
    }
}